﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HospitalManager.QuanTriHeThong.ThietLapHeThong
{
    public partial class TaiKhoan_ChucVu_PhongBan : Page
    {
        private const int PageSize = 20;

        private Module.QuanTriHeThong.ThietLapHeThong.TaiKhoan_ChucVu_PhongBan _taiKhoanChucVuPhongBan = new Module.QuanTriHeThong.ThietLapHeThong.TaiKhoan_ChucVu_PhongBan();
        private int _totalrow;
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.GetPostBackEventReference(this, string.Empty);
            if (Request.Form["__EVENTTARGET"] == "LoadPage")
            {
                LoadPage();
            }
            if (IsPostBack) return;
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                LoadPage();
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["CheckRefresh"] = Session["CheckRefresh"];
        }

        private void LoadPage()
        {
            _taiKhoanChucVuPhongBan = new Module.QuanTriHeThong.ThietLapHeThong.TaiKhoan_ChucVu_PhongBan();
            if (hfTimKiem.Value.Equals("true"))
            {
                //_taiKhoanChucVuPhongBan.TaiKhoan = txtMaTinhThanh.Text.Trim();
                //_taiKhoanChucVuPhongBan.MaChucVu = txtTenTinhThanh.Text.Trim();
                //_taiKhoanChucVuPhongBan.MaPhongBan=
            }
            gvDanhSach.DataSource = _taiKhoanChucVuPhongBan.SelectAll(_taiKhoanChucVuPhongBan, Convert.ToInt32(hfPageIndex.Value.Trim()), PageSize, out _totalrow);
            gvDanhSach.DataBind();
            GeneratePager(_totalrow, PageSize, Convert.ToInt32(hfPageIndex.Value.Trim()));
            Session["CheckRefresh"] = Server.UrlEncode(DateTime.Now.ToString(CultureInfo.InvariantCulture));
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                if (Session["CheckRefresh"].ToString() != ViewState["CheckRefresh"].ToString())
                {
                    LoadPage();
                }
                else
                {
                    var id = ((LinkButton)sender).CommandArgument;
                    _taiKhoanChucVuPhongBan.DeleteById(id);LoadPage();
                }
            }
        }

        protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName != "PageNo") return;
            hfPageIndex.Value = e.CommandArgument.ToString();
            LoadPage();
            e.Item.Attributes.Add("disabled", "disabled");
        }

        public void GeneratePager(int totalRowCount, int pageSize, int currentPage)
        {
            const int totalLinkInPage = 4;
            var totalPageCount = (int)Math.Ceiling((decimal)totalRowCount / pageSize);

            var startPageLink = Math.Max(currentPage - (int)Math.Floor((decimal)totalLinkInPage / 2), 1);
            var lastPageLink = Math.Min(startPageLink + totalLinkInPage - 1, totalPageCount);

            if ((startPageLink + totalLinkInPage - 1) > totalPageCount)
            {
                lastPageLink = Math.Min(currentPage + (int)Math.Floor((decimal)totalLinkInPage / 2), totalPageCount);
                startPageLink = Math.Max(lastPageLink - totalLinkInPage + 1, 1);
            }

            var pageLinkContainer = new List<ListItem>();

            if (startPageLink != 1)
            {
                pageLinkContainer.Add(new ListItem("«", "1", currentPage != 1));
            }
            for (var i = startPageLink; i <= lastPageLink; i++)
            {
                pageLinkContainer.Add(new ListItem(i.ToString(CultureInfo.InvariantCulture),
                    i.ToString(CultureInfo.InvariantCulture), currentPage != i));
            }
            if (lastPageLink != totalPageCount)
                pageLinkContainer.Add(new ListItem("»", totalPageCount.ToString(CultureInfo.InvariantCulture),
                    currentPage != totalPageCount));

            dlPager.DataSource = pageLinkContainer;
            dlPager.DataBind();
        }

        protected void gvDanhSach_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (var i = gvDanhSach.Rows.Count - 1; i > 0; i--)
            {
                var row = gvDanhSach.Rows[i];
                var previousRow = gvDanhSach.Rows[i - 1];
                if (row.Cells[1].Text != previousRow.Cells[1].Text) continue;
                if (row.Cells[1].RowSpan == 0)
                {
                    if (row.Cells[3].Text == previousRow.Cells[3].Text)
                    {
                        if (row.Cells[3].RowSpan == 0)
                        {
                            if (row.Cells[4].Text == previousRow.Cells[4].Text)
                            {
                                if (row.Cells[4].RowSpan == 0)
                                {
                                    previousRow.Cells[4].RowSpan = 2;
                                }
                                else
                                {
                                    previousRow.Cells[4].RowSpan = row.Cells[4].RowSpan + 1;
                                }
                                row.Cells[4].Visible = false;
                                previousRow.Cells[3].RowSpan = previousRow.Cells[4].RowSpan;
                            }
                            else
                            {
                                previousRow.Cells[3].RowSpan = 2;
                            }
                        }
                        else
                        {
                            if (row.Cells[4].Text == previousRow.Cells[4].Text)
                            {
                                if (row.Cells[4].RowSpan == 0)
                                {
                                    previousRow.Cells[4].RowSpan = 2;
                                }
                                else
                                {
                                    previousRow.Cells[4].RowSpan = row.Cells[4].RowSpan + 1;
                                }
                                row.Cells[4].Visible = false;
                            }
                            previousRow.Cells[3].RowSpan = row.Cells[3].RowSpan + 1;
                        }
                        row.Cells[3].Visible = false;
                        previousRow.Cells[1].RowSpan = previousRow.Cells[3].RowSpan;
                        previousRow.Cells[2].RowSpan = previousRow.Cells[3].RowSpan;
                    }
                    else
                    {
                        previousRow.Cells[1].RowSpan = 2;
                        previousRow.Cells[2].RowSpan = 2;
                    }
                }
                else
                {
                    if (row.Cells[3].Text == previousRow.Cells[3].Text)
                    {
                        if (row.Cells[3].RowSpan == 0)
                        {
                            if (row.Cells[4].Text == previousRow.Cells[4].Text)
                            {
                                if (row.Cells[4].RowSpan == 0)
                                {
                                    previousRow.Cells[4].RowSpan = 2;
                                }
                                else
                                {
                                    previousRow.Cells[4].RowSpan = row.Cells[4].RowSpan + 1;
                                }
                                row.Cells[4].Visible = false;
                                previousRow.Cells[3].RowSpan = previousRow.Cells[4].RowSpan;
                            }
                            else
                            {
                                previousRow.Cells[3].RowSpan = 2;
                            }
                        }
                        else
                        {
                            if (row.Cells[4].Text == previousRow.Cells[4].Text)
                            {
                                if (row.Cells[4].RowSpan == 0)
                                {
                                    previousRow.Cells[4].RowSpan = 2;
                                }
                                else
                                {
                                    previousRow.Cells[4].RowSpan = row.Cells[4].RowSpan + 1;
                                }
                                row.Cells[4].Visible = false;
                            }
                            previousRow.Cells[3].RowSpan = row.Cells[3].RowSpan + 1;
                        }
                        row.Cells[3].Visible = false;
                    }
                    previousRow.Cells[1].RowSpan = row.Cells[1].RowSpan + 1;
                    previousRow.Cells[2].RowSpan = row.Cells[2].RowSpan + 1;
                }
                row.Cells[1].Visible = false;
                row.Cells[2].Visible = false;
            }
            if (e.Row.RowType != DataControlRowType.DataRow) return;
            var id = ((DataRowView)e.Row.DataItem)["ID"].ToString();
            if (string.IsNullOrEmpty(id)) return;
            e.Row.Cells[6].FindControl("btnPhanQuyen").Visible = false;
            e.Row.Cells[6].FindControl("btnChuyenCongTac").Visible = true;
            e.Row.Cells[6].FindControl("btnDelete").Visible = true;
        }
    }
}